'use strict';
// const hmac = require("hmac")
// const hashlib = require("hashlib")
const crypto = require('crypto');

module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/xcxzf/kh/hqcs.js 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			pid,
			out_trade_no,
			total_amount,
			subject,
			body,
			valid_time
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		// 解密 sessionKey
		// let decryptedRes = vk.crypto.aes.decrypt({
		// 	data: encryptedKey, // 待解密的原文
		// });
		// let sessionKey = decryptedRes.sessionKey;
		
		// console.log("sessionKey",sessionKey);
		
		// 查询用户_id
		let res1 = await vk.baseDao.selects({
			dbName: "a_recharge",
			// 主表where条件
			whereJson: {
				out_trade_no:out_trade_no
			},
		})
		
		console.log("pid",pid);

		// 数据库查询 对应小程序的虚拟支付参数
		let result = await vk.baseDao.selects({
			dbName: "vk-pay-config",
			// 主表where条件
			whereJson: {
				_id:pid
			},
		})
		console.log("result",result);
		
		let Token = result.rows[0].ttpay["mp-douyin"].Token;
		let Salt = result.rows[0].ttpay["mp-douyin"].Salt;
		let appid = result.rows[0].ttpay["mp-douyin"].appId;
		let notify_url = result.rows[0].ttpay["mp-douyin"].url;
		
		
		res.data={
			app_id: appid,
			out_order_no: data.out_trade_no,
			total_amount: data.total_amount,
			subject: data.subject,
			body: data.body,
			valid_time: data.valid_time,
			token: Token,
			notify_url: notify_url,
			salt: Salt
		}
		
		return res;


		

		
	}
}